/* This is a JPanel class that will host the graphical representation of the 
 sorting methods nad show step by step how they work. */


/* Author: Mark Mckillion, Chris Mowbray
 * Date: 17/04/2012
 * Version 1.0.*/
package assignment4;

import javax.swing.*;
import java.awt.*;

@SuppressWarnings("serial")
public class DrawingPanel extends JPanel
{   /*This class will override the paintComponent method to show the GUI.
     *information will be used from ControlPanel to pass in values.*/
    
    //create an interger which will be used to paint a green tick when an object is sorted.
    protected static int sn;
    
    //create integers that will determine where the black arrows will be drawn.
    protected static int comp1, comp2 = 0;
        
    //create a redraw method which will repaint the panel.
    public void reDraw()
    {   repaint();
    }
    
    //override paint component to create the GUI.
    @Override
    public void paintComponent(Graphics g)
    {   //call to the super class.
        super.paintComponent(g);
        
        //create integers which will determain the size of the array being used.
        int num = Colour.colors.length-1;
        int num2 = Vehicle.vehicles.length-1;
        
        /* create the green tick and black arrow images that will be used.
         * these images are saved within our project file to allow the progam to
         * access them. */
        Image image1 = Toolkit.getDefaultToolkit().getImage("green_tick.jpg");
        Image image2 = Toolkit.getDefaultToolkit().getImage("black-arrow.png");
        
        //Set up the graphic that will be used within the program.
        Graphics2D g1= (Graphics2D)g; 
        
        //if vehicle is chosen draw the array of images.
        if("Vehicle".equals(ControlPanel.str))
        {   //create initial values of the x and y coordinates.
            int x = 55;
            int y = 100;
            
            //if bubble sort has been selected
         if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Bubble"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the num2 value to draw the green tick from right to left 
                    //as the array is sorted.
                    g1.drawImage(image1, x+(num2*140)+15, y+120, this); 
                    num2--;
                }
            }
           
            //if selection sort has been selected
         if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Selection"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the i2 value to draw the green tick from left to right as 
                    //the array is sorted.
                    g1.drawImage(image1, x+(i2*140)+15, y+120, this); 
                }
            }
            
            //if insertion sort has been selected
         if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Insertion"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the i2 value to draw the green tick from left to right as 
                    //the array is sorted.
                    g1.drawImage(image1, x+(i2*140)+15, y+120, this); 
                }
            }
            
            //drawing the array and the arrows which show how items are being sorted.
         for(int i = 0; i < Vehicle.vehicles.length; i++)
            {   //draw all of the vehicles within the array.
                g1.drawImage(Vehicle.vehicles[i], x+(i*140), y, this);
                
                //draw the arrows that show how the sort method works.
                g1.drawImage(image2, x+(comp1*140)+35, 50, this);
                g1.drawImage(image2, x+(comp2*140)+35, 50, this); 
                
                //create a pen and set the colour to draw a line.
                BasicStroke pen = new BasicStroke(3F);
                g1.setColor(Color.BLACK);
                g1.setStroke(pen);
                
                //draw the line that the arrows will move across.
                g1.drawLine(50, 50, x+i*140+75, 50);
            }
        }
        
        //if colour is chosen draw the array of images.
        if("Colour".equals(ControlPanel.str))
        {   //create initial values of the x and y coordinates.
            int x = 55;
            int y = 100;
            
            //if bubble sort has been selected
            if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Bubble"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the num2 value to draw the green tick from right to left 
                    //as the array is sorted.
                    g1.drawImage(image1, x+(num*140)+15, y+120, this); 
                    num--;
                }
            }
           
            //if selection sort has been selected
          if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Selection"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the i2 value to draw the green tick from left to right 
                    //as the array is sorted.
                    g1.drawImage(image1, x+(i2*140)+15, y+120, this); 
                }
            }
            
            //if insertion sort has been selected
          if(String.valueOf(ControlPanel.jComboBox1.getSelectedItem()).equals("Insertion"))
            {   for(int i2 = 0; i2 < sn; i2++)
                {   //this uses the i2 value to draw the green tick from left to right as 
                    //the array is sorted.
                    g1.drawImage(image1, x+(i2*140)+15, y+120, this); 
                }
            }
            
            //drawing the array and the arrows which show how items are being sorted.
            for(int i = 0; i < Colour.colors.length; i++)
            {   //draw all of the colours within the array.
                g1.setColor(Colour.colors[i]);
                g1.fillOval(x+(i*140), y, 80, 80);
                
                //draw the arrows that show how the sort method works.
                g1.drawImage(image2, x+(comp1*140)+35, 50, this);
                g1.drawImage(image2, x+(comp2*140)+35, 50, this); 
                
                //create a pen and set the colour to draw a line.
                BasicStroke pen = new BasicStroke(3F);
                g1.setColor(Color.BLACK);
                g1.setStroke(pen);
                
                //draw the line that the arrows will move across.
                g1.drawLine(50, 50, x+i*140+75, 50);
            }
        }
    }
}   //End of DrawingPanel class.